1. /* slmkhmlt.cpp by K.Tsuru */
  2. // function ID = 220 DRADIX, BRADIX
  3. /*********************************************************************
  4. SLong and SInteger classes
  5. z = x*y
  6. It provides the Karatsuba's multiplication using recursive calls.
  7. Knuth
  8. x*y=(R^n*x1+x0)*(R^n*y1+y0)
  9. = {R^(2n)+R^n}*(x1*y1) + R^n*(x1-x0)*(y0-y1) +(R^n +1)*(x0*y0)
  10. It is desirable that x and y have the identical size.
  11. **********************************************************************/
  12. #ifndef SN_H
  13. #include "sn.h"
  14. #endif
  15. void SLong::KHHMult(const SLong& x, const SLong& y, SLong& z){
  16. if( (&z == &x) || (&z == &y)){
  17. z.SetError(z.SYNTAX_ERR, "KHHMult", 220);
  18. }
  19. if( !x.Sign(220) || !y.Sign(220) ){
  20. z.SetZero(); return;
  21. }
  22. uint n = x.FFTMaxArraySize()/2;
  23. uint xfig = ceilpow2(x.Head()+1), yfig = ceilpow2(y.Head()+1), k;
  24. if( (xfig <= n) && (yfig <= n) ){
  25. z = x*y; // FFT
  26. return;
  27. }
  28. SLong x0(x.Type(), 0), x1(x.Type(), 0);
  29. SLong y0(x.Type(), 0), y1(x.Type(), 0), u(x.Type(), 0);
  30. // If xfig != yfig, divide x or y into two parts.
  31. if(xfig > yfig){
  32. // (R^n*x1+x0)*y = R^n*(x1*y) + x0*y, where n = xfig/2
  33. SLDivHalf(x, x0, x1);
  34. KHHMult(x0, y, z);
  35. KHHMult(x1, y, u);
  36. z += u.MultPowRdx(xfig/2);
  37. return;
  38. } else if(xfig < yfig){
  39. SLDivHalf(y, y0, y1);
  40. KHHMult(y0, x, z);
  41. KHHMult(y1, x, u);
  42. z += u.MultPowRdx(yfig/2);
  43. return;
  44. }
  45. // Here xfig == yfig is satisfied.
  46. SLDivHalf(x, x0, x1);
  47. SLDivHalf(y, y0, y1);
  48. k = yfig/2;
  49. KHHMult(x0, y0, u);
  50. z = u;
  51. z += u.MultPowRdx(k);
  52. KHHMult( x1 - x0, y0 - y1, u);
  53. z += u.MultPowRdx(k);
  54. KHHMult(x1, y1, u);
  55. z += u.MultPowRdx(k);
  56. z += u.MultPowRdx(k);
  57. z.KaratsubaHHMultUsedTimes++; // counter
  58. return;
  59. }

slmkhmlt.cpp : last modifiled at 2017/03/04 21:08:48(1,720 bytes)
created at 2017/10/07 10:26:50
The creation time of this html file is 2017/11/09 14:52:03 (Thu Nov 09 14:52:03 2017).